<template>
  <el-dialog
    :title="type === 'add' ? '创建' : '修改'"
    :close-on-click-modal="false"
    @close="() => cancel()"
    :visible.sync="visible">
    <el-form :model="dataSource" :rules="dataRule" ref="brand" @keyup.enter.native="saveOrUpdate()" label-width="200px">
    <el-form-item label="品牌名" prop="name">
      <el-input v-model="dataSource.name" placeholder="品牌名" />
    </el-form-item>
    <el-form-item label="品牌logo地址" prop="logo">
      <SingleUpload v-model="dataSource.logo"></SingleUpload>
    </el-form-item>
    <el-form-item label="介绍" prop="descript">
      <el-input v-model="dataSource.descript" placeholder="介绍" />
    </el-form-item>
    <el-form-item label="检索首字母" prop="firstLetter">
      <el-input v-model="dataSource.firstLetter" placeholder="检索首字母" />
    </el-form-item>
    <el-form-item label="排序" prop="sort">
      <el-input v-model.number="dataSource.sort" placeholder="排序" />
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="cancel">取消</el-button>
      <el-button type="primary" @click="saveOrUpdate()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import SingleUpload from '@/components/upload/singleUpload'
import {saveBrand, updateBrand} from '@/api/product/brand'

export default {
  components: {
    SingleUpload
  },
  data () {
    return {
      visible: false,
      type: 'add',
      dataSource: {
        brandId: null,
        name: '',
        logo: '',
        descript: '',
        firstLetter: '',
        sort: 0
      },
      dataRule: {
        name: [
          { required: true, message: '品牌名不能为空', trigger: 'blur' }
        ],
        logo: [
          { required: true, message: '品牌logo地址不能为空', trigger: 'blur' }
        ],
        descript: [
          { required: true, message: '介绍不能为空', trigger: 'blur' }
        ],
        firstLetter: [
          { validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error('首字母必须填写'))
            } else if (!/^[a-zA-Z]$/.test(value)) {
              callback(new Error('首字母必须a-z或者A-Z之间'))
            } else {
              callback()
            }
          },
            trigger: 'blur' }
        ],
        sort: [
          { validator: (rule, value, callback) => {
            if (value === '') {
              callback(new Error('排序字段必须填写'))
            } else if (!Number.isInteger(value) || value < 0) {
              callback(new Error('排序字段必须是个整数'))
            } else {
              callback()
            }
          },
            trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    init (row) {
      this.visible = true
      if (row) {
        this.type = 'edit'
        this.$nextTick(() => {
          this.dataSource = {...row}
        })
      } else {
        this.dataSource = {
          brandId: null,
          name: '',
          logo: '',
          descript: '',
          firstLetter: '',
          sort: 0
        }
        this.type = 'add'
      }
    },
    // 表单提交
    saveOrUpdate () {
      this.$refs['brand'].validate((valid) => {
        if (valid) {
          if (this.type === 'add') {
            saveBrand(this.dataSource).then(() => {
              this.reset()
            })
          } else {
            updateBrand(this.dataSource).then(() => {
              this.reset()
            })
          }
          this.visible = false
        }
      })
    },
    reset () {
      this.$emit('refresh')
      this.$refs.brand.resetFields()
    },
    cancel () {
      this.visible = false
      this.$refs.brand.resetFields()
    }
  }
}
</script>
